<?php
/**
 * Created by PhpStorm.
 * User: N
 * Date: 2017/12/13
 * Time: 15:17
 */
namespace common\models;


use Yii;
use yii\base\Model;
use common\models\Models;
use backend\modules\content\models\Sitemodel;
use backend\modules\content\models\Modelfield;
use yii\db\ActiveRecord;
use yii\data\ActiveDataProvider;
use yii\data\Pagination;
use backend\modules\admin\models\Admin;
use yii\behaviors\TimestampBehavior;



/**
 * This is the model class for table "category".
 *
 * @property string $catid
 * @property string $siteid
 **/
class Notice extends ActiveRecord
{

     /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%notice}}';
    }

    /**
    * 验证规则
    */
    public function rules()
    {
        return [
            [['title'],'required','message'=>'不能为空'],
            [['title','content'],'string','message'=>'格式不正确'], 
            [['msg_id','from_id','to_id','add_time','last_update','type_id','status','isadmin'],'number','message'=>'格式不正确'],   
        ];
    }

//    public function behaviors()
//    {
//
//        return [
//            [
//                'class' => TimestampBehavior::className(),
//                'createdAtAttribute' => 'add_time',// 自己根据数据库字段修改
//                'updatedAtAttribute' => 'last_update', // 自己根据数据库字段修改
//            ],
//        ];
//    }

    public function beforeSave($insert){
        if($insert){
            $this->status=0;
            $this->add_time = time();
            $this->last_update = time();
            //插入时补充操作
            //$this->inputtime = time();
        }else{
            //修改时补充操作
            $this->last_update = time();

        }
        return true;

    }



        /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'title' => '标题',
            'status' => '状态',
            'content' => '内容',
            'from_id' => '通知发送人',
            'to_id' => '通知接收人',
            'type_id' => '通知类型'
        ];
    }

    public function getTypename()
    {
        if($this->type_id >0){
            $ntypemodel = Ntype::find()->where(['type_id' => $this->type_id])->one();
            return $ntypemodel['type_name'];
        }
    }

    public function getFromname()
    {
        if($this->isadmin == 1){
            $adminmodel = Admin::find()->where(['userid' => $this->from_id])->one();
            return $adminmodel['username'];
        }elseif($this->isadmin == 0){
            $usermodel = User::find()->where(['id' => $this->from_id])->one();
            return $usermodel['realname'];
        }
    }

    /**
     * 获取信息接受者名称，优先realname
     * @return mixed|string
     */
    public function getToname()
    {
        $user = Admin::find()->where(['userid' => $this->to_id])->select('realname,username')->one();
        return $user['realname']?$user['realname']:($user['realname']?$user['realname']:'--');
    }

    public function getStatusnow()
    {
        switch($this->status){
            case -1:
                return '发送失败';
                break;
            case 0:
                return '发送成功未查看';
                break;
            case 1:
                return '发送成功已查看';
            default:
                return '发送状态异常';
        }

    }

    public function getNtype()
    {
        return $this->hasOne(Ntype::className(), ['type_id' => 'type_id']);
    }

    /**
     * 获取当前用户未查看的通知
     */
    public static function getnotice()
    {
        $adminuser = Yii::$app->session['adminuser'];
        $num = Notice::find()->where(['to_id'=>$adminuser->userid,'status'=>'0'])->count();
        $notice = Notice::find()->select('msg_id,title,add_time')->where(['to_id'=>$adminuser->userid,'status'=>'0'])->orderBy('add_time DESC')->limit('4')->asArray()->all();
        $data['notice'] = $notice;
        $data['num'] = $num;
        return $data;
    }

}